package {
	import flexunit.flexui.TestRunnerBase;
	import flexunit.framework.Test;
	import flexunit.framework.TestCase;
	import flexunit.framework.TestSuite;

	public class TestRunner {

		public static function run(testRunner:TestRunnerBase, testCases:Array):void {
			testRunner.test = createTestSuite(testCases);
			testRunner.startTest();
		}

		private static function addTestMethods(suite:TestSuite, clazz:Class, testInstance:Test):void {
			var methodNames:Array = testInstance.getTestMethodNames();
			
			for (var i:int = 0; i < methodNames.length; i++) {
				addTestMethod(suite, clazz, String(methodNames[i]));
			}
		}

		private static function addTestMethod(suite:TestSuite, clazz:Class, methodName:String):void {
			var test:Test = new clazz();
			
			if (test is TestCase) {
				TestCase(test).methodName = methodName;
			}
			suite.addTest(test);
		}

		private static function createTestSuite(testClasses:Array):TestSuite {
			var suite:TestSuite = new TestSuite();

			for each (var testClass:Class in testClasses) {
				var newTest:Test = new testClass();
				addTestMethods(suite, testClass, newTest);
			}
			return suite;
		}
	}
}